﻿using Mt.Site.Application.Organizations.Dto;
using System.Collections.Generic;
using System.Linq;

namespace Mt.Site.Application.Organizations
{
    public static class OrganizationUnitHelper
    {
        public static List<SimpleOrganinzationUnitDto> AdapterList(List<SimpleOrganinzationUnitDto> list)
        {
            //移除list中缺少上级的项
            for (int i = 0; i < list.Count; i++)
            {
                var unit = list[i];
                var codeList = unit.Code.Split('.');

                var str = "";
                foreach (var item in codeList)
                {
                    if (str != "")
                    {
                        str += ".";
                    }
                    str += item;

                    var superiorUnit = list.FirstOrDefault(c => c.Code == str);
                    if (superiorUnit == null)
                    {
                        list.RemoveAll(m => m.Code.StartsWith(str));
                        i = i - 1;
                        break;
                    }
                }
            }
            return list;
        }

        public static List<OrganizationUnitDto> AdapterList(List<OrganizationUnitDto> list)
        {
            //移除list中缺少上级的项
            for (int i = 0; i < list.Count; i++)
            {
                var unit = list[i];
                var codeList = unit.Code.Split('.');

                var str = "";
                foreach (var item in codeList)
                {
                    if (str != "")
                    {
                        str += ".";
                    }
                    str += item;

                    var superiorUnit = list.FirstOrDefault(c => c.Code == str);
                    if (superiorUnit == null)
                    {
                        list.RemoveAll(m => m.Code.StartsWith(str));
                        i = i - 1;
                        break;
                    }
                }
            }
            return list;
        }
    }
}
